home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Utilities / Calc / calc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-10  |  11.3 KB  |  361 lines  |  [TEXT/????]

  1. /*
  2.  * Copyright (c) 1992 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Definitions for calculator program.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <setjmp.h>
  11. #include "xmath.h"
  12.  
  13.  
  14. /*
  15.  * Configuration definitions
  16.  */
  17. #define    CALCPATH    "CALCPATH"    /* environment variable for files */
  18. #define    CALCRC        "CALCRC"    /* environment variable for startup */
  19. #define    HOME        "HOME"        /* environment variable for home dir */
  20. #define    PAGER        "PAGER"        /* environment variable for help */
  21. #define    SHELL        "SHELL"        /* environment variable for shell */
  22. #define DEFAULTCALCHELP    "help"        /* help file that -h prints */
  23. #define DEFAULTSHELL    "sh"        /* default shell to use */
  24. #define    CALCEXT        ".cal"    /* extension for files read in */
  25. #define    PATHSIZE    1024    /* maximum length of path name */
  26. #define    HOMECHAR    '~'    /* char which indicates home directory */
  27. #define DOTCHAR        '.'    /* char which indicates current directory */
  28. #define    PATHCHAR    '/'    /* char which separates path components */
  29. #define    LISTCHAR    ':'    /* char which separates paths in a list */
  30. #define    MAXCMD        1024    /* maximum length of command invocation */
  31. #define    MAXERROR    512    /* maximum length of error message string */
  32.  
  33. #define    SYMBOLSIZE    256    /* maximum symbol name size */
  34. #define    MAXINDICES    20    /* maximum number of indices for objects */
  35. #define    MAXLABELS    100    /* maximum number of user labels in function */
  36. #define    MAXOBJECTS    10    /* maximum number of object types */
  37. #define MAXDIM        4    /* maximum number of dimensions in matrices */
  38. #define    MAXSTRING    1024    /* maximum size of string constant */
  39. #define    MAXSTACK    1000    /* maximum depth of evaluation stack */
  40. #define    MAXFILES    20    /* maximum number of opened files */
  41. #define PROMPT1        "> "    /* normal prompt */
  42. #define PROMPT2        ">> "    /* prompt when inside multi-line input */
  43.  
  44.  
  45. #define    TRACE_NORMAL    0x00    /* normal trace flags */
  46. #define    TRACE_OPCODES    0x01    /* trace every opcode */
  47. #define    TRACE_NODEBUG    0x02    /* suppress debugging opcodes */
  48. #define    TRACE_MAX    0x03    /* maximum value for trace flag */
  49.  
  50. #define DISPLAY_DEFAULT 20    /* default digits for float display */
  51. #define EPSILON_DEFAULT "1e-20"    /* allowed error for float calculations */
  52. #define MAXPRINT_DEFAULT 16    /* default number of elements printed */
  53. #define USUAL_ELEMENTS    4    /* usual number of elements for object */
  54.  
  55. #define ABORT_NONE    0    /* abort not needed yet */
  56. #define ABORT_STATEMENT    1    /* abort on statement boundary */
  57. #define ABORT_OPCODE    2    /* abort on any opcode boundary */
  58. #define ABORT_MATH    3    /* abort on any math operation */
  59. #define ABORT_NOW    4    /* abort right away */
  60.  
  61. #define CONFIG_MODE    1    /* types of configuration parameters */
  62. #define CONFIG_DISPLAY    2
  63. #define CONFIG_EPSILON    3
  64. #define CONFIG_TRACE    4
  65. #define CONFIG_MAXPRINT    5
  66. #define    CONFIG_MUL2    6
  67. #define    CONFIG_SQ2    7
  68. #define    CONFIG_POW2    8
  69. #define    CONFIG_REDC2    9
  70.  
  71.  
  72. /*
  73.  * Flags to modify results from the printvalue routine.
  74.  * These flags are OR'd together.
  75.  */
  76. #define    PRINT_NORMAL    0x00    /* print in normal manner */
  77. #define    PRINT_SHORT    0x01    /* print in short format (no elements) */
  78. #define    PRINT_UNAMBIG    0x02    /* print in non-ambiguous manner */
  79.  
  80.  
  81. /*
  82.  * Definition of values of various types.
  83.  */
  84. typedef struct value VALUE;
  85. typedef struct object OBJECT;
  86. typedef struct matrix MATRIX;
  87. typedef struct list LIST;
  88. typedef    long FILEID;
  89.  
  90.  
  91. struct value {
  92.     short v_type;            /* type of value */
  93.     short v_subtype;        /* other data related to some types */
  94.     union {
  95.         long vv_int;        /* small integer value */
  96.         FILEID vv_file;        /* id of opened file */
  97.         NUMBER *vv_num;        /* arbitrary sized numeric value */
  98.         COMPLEX *vv_com;    /* complex number */
  99.         VALUE *vv_addr;        /* address of variable value */
  100.         MATRIX *vv_mat;        /* address of matrix */
  101.         LIST *vv_list;        /* address of list */
  102.         OBJECT *vv_obj;        /* address of object */
  103.         char *vv_str;        /* string value */
  104.     } v_union;
  105. };
  106.  
  107.  
  108. /*
  109.  * For ease in referencing
  110.  */
  111. #define v_int    v_union.vv_int
  112. #define    v_file    v_union.vv_file
  113. #define v_num    v_union.vv_num
  114. #define v_com    v_union.vv_com
  115. #define v_addr    v_union.vv_addr
  116. #define v_str    v_union.vv_str
  117. #define v_mat    v_union.vv_mat
  118. #define    v_list    v_union.vv_list
  119. #define v_obj    v_union.vv_obj
  120. #define    v_valid    v_union.vv_int
  121.  
  122.  
  123. /*
  124.  * Value types.
  125.  */
  126. #define V_NULL    0    /* null value */
  127. #define V_INT    1    /* normal integer */
  128. #define V_NUM    2    /* number */
  129. #define V_COM    3    /* complex number */
  130. #define V_ADDR    4    /* address of variable value */
  131. #define V_STR    5    /* address of string */
  132. #define V_MAT    6    /* address of matrix structure */
  133. #define    V_LIST    7    /* address of list structure */
  134. #define V_OBJ    8    /* address of object structure */
  135. #define    V_FILE    9    /* opened file id */
  136. #define V_MAX    9    /* highest legal value */
  137.  
  138. #define V_STRLITERAL    0    /* string subtype for literal str */
  139. #define V_STRALLOC    1    /* string subtype for allocated str */
  140.  
  141. #define TWOVAL(a,b) ((a) * (V_MAX+1) + (b))    /* for switch of two values */
  142.  
  143.  
  144. /*
  145.  * Structure of a matrix.
  146.  */
  147. struct matrix {
  148.     long m_dim;        /* dimension of matrix */
  149.     long m_size;        /* total number of elements */
  150.     long m_min[MAXDIM];    /* minimum bound for indices */
  151.     long m_max[MAXDIM];    /* maximum bound for indices */
  152.     VALUE m_table[1];    /* actually varying length table */
  153. };
  154.  
  155. #define matsize(n) (sizeof(MATRIX) - sizeof(VALUE) + ((n) * sizeof(VALUE)))
  156.  
  157.  
  158. /*
  159.  * List definitions.
  160.  * An individual list element.
  161.  */
  162. typedef struct listelem LISTELEM;
  163. struct listelem {
  164.     LISTELEM *e_next;    /* next element in list (or NULL) */
  165.     LISTELEM *e_prev;    /* previous element in list (or NULL) */
  166.     VALUE e_value;        /* value of this element */
  167. };
  168.  
  169.  
  170. /*
  171.  * Structure for a list of elements.
  172.  */
  173. struct list {
  174.     LISTELEM *l_first;    /* first list element (or NULL) */
  175.     LISTELEM *l_last;    /* last list element (or NULL) */
  176.     LISTELEM *l_cache;    /* cached list element (or NULL) */
  177.     long l_cacheindex;    /* index of cached element (or undefined) */
  178.     long l_count;        /* total number of elements in the list */
  179. };
  180.  
  181. extern void insertlistfirst(), insertlistlast(), insertlistmiddle();
  182. extern void removelistfirst(), removelistlast(), removelistmiddle();
  183. extern void listfree(), listprint();
  184. extern long listsearch(), listrsearch();
  185. extern BOOL listcmp();
  186. extern VALUE *listindex();
  187. extern LIST *listalloc(), *listcopy();
  188.  
  189.  
  190. /*
  191.  * Object actions.
  192.  */
  193. #define OBJ_PRINT    0    /* print the value */
  194. #define OBJ_ONE        1    /* create the multiplicative identity */
  195. #define OBJ_TEST    2    /* test a value for "zero" */
  196. #define OBJ_ADD        3    /* add two values */
  197. #define OBJ_SUB        4    /* subtrace one value from another */
  198. #define OBJ_NEG        5    /* negate a value */
  199. #define OBJ_MUL        6    /* multiply two values */
  200. #define OBJ_DIV        7    /* divide one value by another */
  201. #define OBJ_INV        8    /* invert a value */
  202. #define OBJ_ABS        9    /* take absolute value of value */
  203. #define OBJ_NORM    10    /* take the norm of a value */
  204. #define OBJ_CONJ    11    /* take the conjugate of a value */
  205. #define OBJ_POW        12    /* take the power function */
  206. #define OBJ_SGN        13    /* return the sign of a value */
  207. #define OBJ_CMP        14    /* compare two values for equality */
  208. #define OBJ_REL        15    /* compare two values for inequality */
  209. #define OBJ_QUO        16    /* integer quotient of values */
  210. #define OBJ_MOD        17    /* remainder of division of values */
  211. #define OBJ_INT        18    /* integer part of */
  212. #define OBJ_FRAC    19    /* fractional part of */
  213. #define OBJ_INC        20    /* increment by one */
  214. #define OBJ_DEC        21    /* decrement by one */
  215. #define OBJ_SQUARE    22    /* square value */
  216. #define OBJ_SCALE    23    /* scale by power of two */
  217. #define OBJ_SHIFT    24    /* shift left (or right) by number of bits */
  218. #define OBJ_ROUND    25    /* round to specified decimal places */
  219. #define OBJ_BROUND    26    /* round to specified binary places */
  220. #define OBJ_ROOT    27    /* take nth root of value */
  221. #define OBJ_SQRT    28    /* take square root of value */
  222. #define OBJ_MAXFUNC    28    /* highest function */
  223.  
  224.  
  225. /*
  226.  * Definition of an object type.
  227.  * This is actually a varying sized structure.
  228.  */
  229. typedef struct {
  230.     char *name;            /* name of object */
  231.     int count;            /* number of elements defined */
  232.     int actions[OBJ_MAXFUNC+1];    /* function indices for actions */
  233.     int elements[1];        /* element indexes (MUST BE LAST) */
  234. } OBJECTACTIONS;
  235.  
  236. #define objectactionsize(elements) \
  237.     (sizeof(OBJECTACTIONS) + ((elements) - 1) * sizeof(int))
  238.  
  239.  
  240. /*
  241.  * Structure of an object.
  242.  * This is actually a varying sized structure.
  243.  * However, there are always at least USUAL_ELEMENTS values in the object.
  244.  */
  245. struct object {
  246.     OBJECTACTIONS *o_actions;    /* action table for this object */
  247.     VALUE o_table[USUAL_ELEMENTS];    /* object values (MUST BE LAST) */
  248. };
  249.  
  250. #define objectsize(elements) \
  251.     (sizeof(OBJECT) + ((elements) - USUAL_ELEMENTS) * sizeof(VALUE))
  252.  
  253.  
  254. /*
  255.  * File ids corresponding to standard in, out, error, and when not in use.
  256.  */
  257. #define    FILEID_STDIN    0
  258. #define    FILEID_STDOUT    1
  259. #define    FILEID_STDERR    2
  260. #define    FILEID_NONE    -1
  261.  
  262.  
  263. /*
  264.  * Common definitions
  265.  */
  266. extern long maxprint;        /* number of elements to print */
  267. extern int abortlevel;        /* current level of aborts */
  268. extern BOOL inputwait;        /* TRUE if in a terminal input wait */
  269. extern FLAG traceflags;        /* tracing flags */
  270. extern VALUE *stack;        /* execution stack */
  271. extern jmp_buf jmpbuf;        /* for errors */
  272.  
  273. extern char *calcpath;        /* $CALCPATH or default */
  274. extern char *calcrc;        /* $CALCRC or default */
  275. extern char *home;        /* $HOME or default */
  276. extern char *shell;        /* $SHELL or default */
  277.  
  278. /*
  279.  * Functions.
  280.  */
  281. extern MATRIX *matadd(), *matsub(), *matmul(), *matneg();
  282. extern MATRIX *matalloc(), *matcopy(), *matsquare(), *matinv();
  283. extern MATRIX *matscale(), *matmulval(), *matpowi(), *matconj(), *matquoval();
  284. extern MATRIX *matmodval(), *matint(), *matfrac(), *matround(), *matbround();
  285. extern MATRIX *mattrans(), *matcross(), *matshift();
  286. extern BOOL mattest(), matcmp();
  287. extern long matsearch(), matrsearch();
  288. extern VALUE matdet(), matdot();
  289. extern void matfill(), matfree(), matprint();
  290.  
  291. #if 0
  292. extern BOOL matisident();
  293. #endif
  294.  
  295. extern OBJECT *objcopy(), *objalloc();
  296. extern VALUE objcall();
  297. extern void objfree();
  298. extern void objuncache();
  299. extern int addelement();
  300. extern int defineobject();
  301. extern int checkobject();
  302. extern void showobjfuncs();
  303. extern int findelement();
  304. extern int objoffset();
  305.  
  306. extern void freevalue(), copyvalue(), negvalue(), addvalue(), subvalue();
  307. extern void mulvalue(), squarevalue(), invertvalue(), roundvalue();
  308. extern void broundvalue(), intvalue(), fracvalue(), incvalue(), decvalue();
  309. extern void conjvalue(), sqrtvalue(), rootvalue(), absvalue(), normvalue();
  310. extern void shiftvalue(), scalevalue(), powivalue(), powervalue();
  311. extern void divvalue(), quovalue(), modvalue(), printvalue();
  312. extern BOOL testvalue(), comparevalue();
  313. extern FLAG relvalue();
  314.  
  315. extern FILEID openid(), indexid();
  316. extern BOOL validid(), errorid(), eofid(), closeid();
  317. extern int getcharid();
  318. extern void idprintf(), printid(), flushid(), readid();
  319.  
  320. extern FILE *f_open();
  321. extern int openstring();
  322. extern int openterminal();
  323. extern int opensearchfile();
  324. extern int nextchar();
  325. extern void reread();
  326.  
  327. extern VALUE builtinfunc();
  328. extern NUMBER *constvalue();
  329. extern long addnumber(), addqconstant();
  330. extern long linenumber();
  331. extern char *builtinname();
  332. extern char *inputname();
  333. extern BOOL inputisterminal();
  334. extern void resetinput();
  335. extern char *nextline();
  336. extern void calculate();
  337. extern void initstack();
  338. extern int dumpop();
  339. extern void version();        /* print version string */
  340. extern void runrcfiles();
  341. extern void getcommands();
  342. extern void givehelp();
  343. extern void setprompt();
  344.  
  345. extern void getconfig();
  346. extern void setconfig();
  347. extern int configtype();
  348.  
  349. #ifdef VARARGS
  350. void error();
  351. #else
  352. # ifdef __STDC__
  353. void error(char *, ...);
  354. # else
  355. void error();
  356. # endif
  357. #endif
  358.  
  359.  
  360. /* END CODE */
  361.